<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var finalPrices = function (prices) {
      const ans = prices.concat();
      for (let n = prices.length, i = n - 1; i >= 0; --i) {
        for (let j = i + 1; j < n; ++j) {
          if (prices[j] <= prices[i]) {
            ans[i] = prices[i] - prices[j];
            break;
          }
        }
      }
      return ans;
    };
    console.log(finalPrices([8, 4, 6, 2, 3]));
  </script>
</body>

</html>